//
//  AddTableViewController.m
//  11210
//
//  Created by student on 16/3/4.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "AddTableViewController.h"

@interface AddTableViewController ()
@property (nonatomic,strong) NSMutableArray *mList;
@end

@implementation AddTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.mList = [@[@"张三",@"李四",@"王五"] mutableCopy];
    UIBarButtonItem *moveBtn = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(toggleMove)];
    self.navigationItem.rightBarButtonItem = moveBtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)toggleMove
{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    [self.navigationItem.rightBarButtonItem setTitle:self.tableView.editing?@"完成":@"编辑"];
    if(self.tableView.editing)
    {
        NSIndexPath *tmppath=[NSIndexPath indexPathForRow:self.mList.count inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[tmppath] withRowAnimation:UITableViewRowAnimationLeft];
    }
    else
    {
        NSIndexPath *tmppath=[NSIndexPath indexPathForRow:self.mList.count inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[tmppath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if(self.tableView.editing)
    {
        return self.mList.count+1;
    }
    return self.mList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    if(indexPath.row==self.mList.count)
    {
        cell.textLabel.text=@"添加新同学";
        
    }
    else
    {
        cell.textLabel.text=self.mList[indexPath.row];
    }
    
    return cell;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==self.mList.count)
    {
        return UITableViewCellEditingStyleInsert;
    }
    else
    {
        return UITableViewCellEditingStyleDelete;
    }
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle==UITableViewCellEditingStyleDelete)
    {
        [self.mList removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    if(editingStyle == UITableViewCellEditingStyleInsert)
    {
        // 1. 数据要添加
        [self.mList addObject:[NSString stringWithFormat:@"新同学%i",self.mList.count]];
        // 2. 新的一行
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }

}


@end
